var webpack = require('webpack'),
    path = require('path'),
    glob = require('glob'),
    precss = require('precss'),
    autoprefixer = require('autoprefixer'),
    ExtractTextPlugin = require('extract-text-webpack-plugin');
/**-----------------------------
 * 环境配置
 --------------------------------*/
// 判断开发环境||生产环境||测试环境
var isDev = function() {
    return process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'hotdev';
};
var isHot = function() {
    return process.env.NODE_ENV === 'hotdev';
};
// 服务器地址
var server = "http://common.demo.qq.tt:8081";

// 输出环境: '/dev/'表示开发环境;'/dist/'表示生产环境
var sEnvironment = function() {
    switch (process.env.NODE_ENV) {
        case 'test':
            return '/test/';
        case 'production':
            return '/dist/';
        default:
            return '/dev/';
    }
};
// 运行终端: 'mobile/'表示微信端;'basic/'表示PC端
var sSystem = 'basic/';

// 业务模块名: * 为遍历所有业务功能模块
var sFunName = '*';

/**------------------------------------
 * 定义插件
 --------------------------------------*/
var plugins = [
    //提供全局的变量，在模块中使用无需用require引入
    new webpack.ProvidePlugin({
        jQuery: "jquery",
        $: "jquery",
        "window.jQuery": "jquery"
    }),
    //提取公共模块
    new webpack.optimize.CommonsChunkPlugin({
        name: 'vendor/vendor',
        filename: '[name].min.js',
        minChunks: 10,
    }),
    //单独打包css
    new ExtractTextPlugin('[name].min.css'),
];
if (!isDev()) {
    plugins.push(
        //文件压缩/混淆
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            },
            mangle: {
                except: ['$', 'webpackJsonpCallback']
            }
        })
    );
}

/**-------------------------------------------
 * 获取多页面的每个入口文件，用于配置中的entry
 ---------------------------------------------*/
function getEntry() {
    var entry = {};
    var nLength = sSystem.length - 1;
    //首先我们先读取我们的开发目录 需求功能member 手机端为mobile PC端为basic

    var srcDirName = './src/' + sSystem + sFunName + '/*/*.js';
    glob.sync(srcDirName).forEach(function(name) {
        var n = name.slice(name.lastIndexOf(sSystem) + nLength, name.length - 3);
        n = n.slice(0, n.lastIndexOf('/'));
        //接着我对路径字符串进行了一些裁剪成想要的路径
        entry[n] = name;
        entry['vendor/vendor'] = [];
    });
    console.log('打包环境' + process.env.NODE_ENV);
    console.log('是否压缩文件：' + !isDev());
    console.log('输出路径：' + sEnvironment() + sSystem);
    return entry;
}

/**--------------
 * webpack参数
 ----------------*/
module.exports = {
    entry: getEntry(),
    output: {
        //输出路径 终端不同时输出路径不同 手机端为mobile PC端为basic
        path: path.join(__dirname, sEnvironment() + sSystem),
        filename: '[name].min.js',
        publicPath: isHot() ? (server + sEnvironment() + sSystem) : '../',
        chunkFilename: '[name].[chunkhash:8].js'
    },
    module: {
        loaders: [{
            test: /\.jsx?$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
                presets: ['es2015', 'react']
            }
        }, {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style', 'css?sourceMap!postcss-loader?')
        }, {
            test: /\.(png|jpe?g|gif|svg)$/,
            loader: 'url?limit=8192&name=images/[name].[ext]'
        }, {
            test: /\.(eot|ttf|woff)$/i,
            loader: 'url?limit=10000&name=fonts/[hash:8].[name].[ext]'
        }, ]
    },
    postcss: function() {
        return [
            precss,
            autoprefixer({
                browsers: '>5%'
            })
        ];
    },
    externals: {
        jquery: 'jQuery',
    },
    plugins: plugins,
    devtool: isDev() ? 'source-map' : null,
};